package com.chenyi.framework.security.interceptor;

import com.chenyi.common.annotation.UserRequired;
import com.chenyi.common.constant.Constants;
import com.chenyi.common.core.domain.entity.SysShop;
import com.chenyi.common.core.domain.entity.SysUser;
import com.chenyi.common.core.domain.model.LoginShop;
import com.chenyi.common.core.domain.model.LoginUser;
import com.chenyi.common.enums.UserStatus;
import com.chenyi.common.exception.BadRequestException;
import com.chenyi.common.utils.StringUtils;
import com.chenyi.framework.web.service.ShopTokenService;
import com.chenyi.shop.domain.SysShopLogin;
import com.chenyi.shop.service.ISysShopLoginService;
import com.chenyi.shop.service.ISysShopService;
import com.chenyi.user.domain.SysUserLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * MobileLoginInterceptor：会员登录拦截器
 *
 * @author WangKh
 */
@Slf4j
@Component
public class ShopLoginInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private ISysShopLoginService shopLoginService;
    @Autowired
    private ShopTokenService tokenService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserRequired annotation;
        if (handler instanceof HandlerMethod){
            annotation =  ((HandlerMethod) handler).getMethodAnnotation(UserRequired.class);
        }else {
            // 如果不是映射到方法直接通过
            return true;
        }
        //如果不需要登陆验证，直接通过
        if(annotation == null){
            return true;
        }
        //需要验证，获取用户凭证
        String token = tokenService.getToken(request);
        if (annotation.getMust() == false && StringUtils.isBlank(token)){
            return true;
        }
        //凭证为空
        if(StringUtils.isBlank(token)){
            throw new BadRequestException(HttpStatus.UNAUTHORIZED,"无token，请重新登录");
        }
        //验证token
        LoginShop loginShop = tokenService.getLoginShop(request);
        if(loginShop == null){
            SysShopLogin shopLogin = shopLoginService.selectShopLoginByToken(tokenService.getJwtToken(token),Constants.LOGIN_FAIL);
            if (shopLogin == null){
                throw new BadRequestException(HttpStatus.UNAUTHORIZED,"凭证失效，请重新登录");
            }else{
                throw new BadRequestException(HttpStatus.UNAUTHORIZED,shopLogin.getMsg());
            }
        }
        SysShop shop = loginShop.getShop();
        if (shop.getStatus().equals(UserStatus.DISABLE.getCode())){
            throw new BadRequestException(HttpStatus.UNAUTHORIZED,"账号已停用，请联系管理员");
        }
        request.setAttribute("shop",loginShop);
        tokenService.verifyToken(loginShop);
        return super.preHandle(request, response, handler);
    }
}
